import interfaces.IPlateFormeAPI;
import interfaces.IPlateformePlugin;

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.StyledDocument;

@SuppressWarnings("serial")
public class EditeurTextePlugin extends JFrame implements IPlateformePlugin {
		
	private JPanel panel;
	private JTextPane textPane;
	private JMenuBar jm;
	private IPlateFormeAPI plateformeAPI;
	
	public EditeurTextePlugin(IPlateFormeAPI api) throws BadLocationException, InstantiationException, IllegalAccessException{
		
		super();
		
		this.panel = new JPanel();
		this.plateformeAPI = api;
		
		this.textPane = new JTextPane();
		StyledDocument sd = (StyledDocument)this.textPane.getDocument();
		String listePlugins = "";
		for(String i : this.plateformeAPI.getNamesOfAvailablePlugins()){
			listePlugins += i + "\n";
		}
		sd.insertString(0, listePlugins, null);
		this.textPane.setDocument(sd);
		this.jm = new JMenuBar();
		
		this.setTitle("Blocnote --  \"The Super Text Editor\""); 
		this.setSize(407,285);
		this.setLocationRelativeTo(null); 
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		//this.panel.setLayout();
		this.panel.setBackground(Color.white);
		this.setContentPane(this.panel);
		
		this.createMenu();
		this.buildContent();
		//this.open();
	}
	 
	private void createMenu() {
		// TODO Auto-generated method stub
		this.jm = new JMenuBar();
		this.jm.setName("jm");
		this.setJMenuBar(this.jm);
		
		JMenu fichier = new JMenu("Fichier");
		fichier.setName("fichier");
		
		JMenuItem nouveau = new JMenuItem("Nouveau");
		//nouveau.addActionListener(new OuvrirFenetre("CreateNewCalendarFrame"));
		
		JMenuItem ouvrir = new JMenuItem("Ouvrir...");
		
		JMenuItem enregistrer = new JMenuItem("Enregistrer");
		JMenuItem enregistrerSous = new JMenuItem("Enregistrer sous...");
		
		JMenuItem quitter = new JMenuItem("Quitter");
		quitter.addActionListener(new FermerFenetre(this));
		
		fichier.add(nouveau);
		fichier.add(ouvrir);
		fichier.add(enregistrer);
		fichier.add(enregistrerSous);
		fichier.add(quitter);
		jm.add(fichier);
		
		JMenu edition = new JMenu("Edition");
		edition.setName("edition");
		
		JMenuItem couper = new JMenuItem("Couper");
		couper.addActionListener(null);
		
		JMenuItem copier = new JMenuItem("Copier");
		copier.addActionListener(null);
		
		JMenuItem coller = new JMenuItem("Coller");
		coller.addActionListener(null);
		
		edition.add(couper);
		edition.add(copier);
		edition.add(coller);
		jm.add(edition);
	}

	private void buildContent() {
		// TODO Auto-generated method stub
		this.textPane.setPreferredSize(new Dimension(400,230));
		JScrollPane js = new JScrollPane(this.textPane);

		this.panel.add(js);
	}

	public void open(){
	    this.setVisible(true);
	}
	
	@Override
	public void dispose(){
		this.plateformeAPI.unloadPlugin(this.getClass().getName());
		super.dispose();
	}
	
	@Override
	public String getNamePlugin() {
		// TODO Auto-generated method stub
		return "Editeur de texte plugin";
	}

	@Override
	public void load() {
		// TODO Auto-generated method stub
		this.open();
	}
}